import Vue from 'vue'
import Vuex from 'vuex'

import mainpage from './mainpage.js';
import caseDetail from './caseDetail.js'
import comment from './comment.js'

import { fetch_all_tags } from '../api/tag.js'

Vue.use(Vuex)

const loginedUser = {
  id: 4,
  union_id: 'oV8g6wF5__aHpAQPncIX0fQXiNUw',
  open_id: 'ok1jF5IzzHuXcBinI-8Sq0PntK2c',
  gender: 0,
  nickName: '光哥',
  fullName: null,
  telNumber: null,
  avatar: null,
  birthday: null
}

const loggerPlug = store => {
  // 当 store 初始化后调用
  store.subscribe((mutation, state) => {
    // 每次 mutation 之后调用
    // console.log("in subscribe:", mutation);
    // mutation 的格式为 { type, payload }
  })
}

const actionPlug = store => {
  store.subscribeAction((action, state) => {
    // console.log("in action:", action)
  })
}


const state = {
  loginedUser: loginedUser,
  isLogined: false,
  // 下面都是全局需要预加载的数据
  tag_list: [],
}

const actions = {
  // 获取所有的标签字典
  fetch_all_tags({
    commit
  }) {
    fetch_all_tags().then(res => {
      commit('setTags', res)
    })
  },
}

const getters = {
  tag_list: (state, getters, rootState) => {
    return state.tag_list;
  },
};

const mutations = {
  // 标签
  setTags(state, payload) {
    state.tag_list = payload
  }
}

const store = new Vuex.Store({
  // strict: process.env.NODE_ENV !== 'production',
  state,
  actions,
  getters,
  mutations,
  modules: {
    // 创建store时声明的模块为静态模块。
    // 不可在今后的过程中使用store.unregisterModule()来动态卸载模块
    mainpage: mainpage,
    caseDetail: caseDetail,
  },
  plugins: [loggerPlug, actionPlug]
})

export default store;
